<?php

namespace app\admin\logic\Order;

use app\admin\common\ServiceResponse;
use app\admin\model\Order;
use app\admin\logic\Order\Index;
use think\facade\Db;

class OrderTransSaveLogic extends Index
{
    use ServiceResponse;

    protected $return;
    public function __construct()
    {
        $this->return = [
            'code' => 500001,
            'data' => [],
            'message' => ''
        ];
    }
    /**
     * @describe
     * @author: Bobo
     * @date: 2025-08-26 18:06:14 
     * @param $orderId 订单ID
     * @param $orderItemId 订单明细ID
     * @param $wholesalId 经销商订单ID
     * @return Renderable
     */
    public function getInventoryTransSaveInfo(int $orderId, int $orderItemId, int $wholesalId = 0)
    {

        $model = new Order();
        $wholesal_order_id = $wholesal_order_item_id = 0;
        if ($wholesalId > 0) { // 从发货订单跳转过来的
            $wholesal_order_id = $wholesalId;
            $order_item_info = Db::name('order_item')
                ->where('id', $orderItemId)
                ->field('id ,order_id,wholesal_id,product_id,is_futures,arrival_date,warehouse_id,real_arrival_date,lock_qty')
                ->find(); // 获取发货订单明细信息
            $wholesal_order_item_result = Db::name('order_item')
                ->where('order_id', $wholesal_order_id)
                ->where('product_id', $order_item_info['product_id'])
                ->where('is_futures', $order_item_info['is_futures'])
                ->where('arrival_date', $order_item_info['arrival_date'])
                ->where('warehouse_id', $order_item_info['warehouse_id'])
                ->field('id')
                ->find(); // 获取发货订单明细ID

            if (!$wholesal_order_item_result) {
                return $this->error(500001, '未找到对应的发货订单明细');
            }

            $wholesal_order_item_id = $wholesal_order_item_result['id'];
        } else {
            $wholesal_order_id = $orderId;
            $wholesal_order_item_id = $orderItemId;
        }

        $orderInfo = $model->alias('o')
            ->join('db_order_item oi', 'o.id = oi.order_id')
            ->join('db_product p', 'oi.product_id = p.id')
            ->leftjoin('db_product_brand pb', 'p.brand = pb.id')
            ->where('o.id', $wholesal_order_id)
            ->where('oi.id', $wholesal_order_item_id)
            ->field('o.*,oi.*,p.cname as product_name,p.code as product_code,pb.id as brand,pb.name as brand_name')
            ->find(); // 获取存为订单信息


        if (!$orderInfo) {
            return $this->error(500001, '订单不存在或订单明细不存在');
        }
        $products = [
            "order_item_id" => $orderInfo['id'],
            "wid" => $orderInfo['warehouse_id'],
            "is_futures" => $orderInfo['is_futures'],
            "arrival_date" => $orderInfo['arrival_date'],
            "real_arrival_date" => $orderInfo['real_arrival_date'],
            "original_price" => $orderInfo['original_price'],
            "actual_price" => $orderInfo['actual_price'],
            "pending_quantity" => $orderInfo['pending_quantity'],
            "pay_quantity" => $orderInfo['quantity'],
            "product_id" => $orderInfo['product_id'],
            "order_id" => $orderInfo['order_id'],
            "exceptionstatus" => $orderInfo['exceptionstatus'],
            "product_abbr" => $orderInfo['product_abbr'],
            "product_name" => $orderInfo['product_name'],
            "product_code" => $orderInfo['product_code'],
            "brand" => $orderInfo['brand'],
            "brand_name" => $orderInfo['brand_name'],
            "hashrate" => $orderInfo['hashrate'],
            "expiration_date" => $orderInfo['expiration_date'],
            "waiting_shipment_quantity" => $orderInfo['pending_quantity'] - $orderInfo['lock_qty'],
        ];


        $order_list = $model->alias('o')
            ->with(['customer'])
            ->join('db_order_item oi', 'o.id = oi.order_id')
            ->where('o.order_type', 2)
            // ->where('o.pay_status', 3)
            ->where(function ($query) use ($wholesalId, $orderInfo) {
                if ($wholesalId > 0) {
                    $query->where('oi.wholesal_id', $wholesalId);
                } else {
                    $query->where('oi.wholesal_id', $orderInfo['order_id']);
                }
            })
            ->where('oi.product_id', $orderInfo['product_id'])
            ->where('oi.is_futures', $orderInfo['is_futures'])
            ->where('oi.warehouse_id', $orderInfo['warehouse_id'])
            ->where('oi.arrival_date', $orderInfo['arrival_date'])
            ->field(
                'o.id,oi.id as order_item_id,o.order_no,o.user_id,o.delivery_method,o.country_name,o.city_name,o.state ,o.address ,o.address2,o.company,o.logistics_fee,"" as shipping_address,
                oi.product_id,oi.pending_quantity as quantity'
            )
            ->select();


        $inventory = $this->getInventory([
            'productid' => $orderInfo['product_id'],
            'wid' => $orderInfo['warehouse_id'],
            'is_futures' => $orderInfo['is_futures'],
            'arrival_date' => $orderInfo['arrival_date'],
        ]);



        return $this->success([
            'order_list' => $order_list,
            'products' => $products,
            'inventory_list' => $inventory['data']
        ]);
    }
}
